from django.db import models
from django import forms


class Delivery( models.Model ):
    origin = models.ForeignKey( 'Address', related_name='origin', blank=True )
    destination = models.ForeignKey( 'Address', related_name='destination', blank=True )
    ready = models.DateTimeField()
    due = models.DateTimeField()
    packages = models.ManyToManyField( 'Package' )
	
    def __unicode__( self ):
        return '%s -> %s' % ( self.origin, self.destination )

    def __repr__( self ):
        return '<Delivery: %s>' % ( self.__unicode__() )


class Package( models.Model ):
    type = models.CharField( max_length=16 )
    size = models.CharField( max_length=16 )
    weight = models.DecimalField( max_digits=5, decimal_places=2 )

    def __unicode__( self ):
        return self.type

    def __repr__( self ):
        return '<Package: %s>' % ( self.__unicode__() )


class Center( models.Model ):
    number = models.CharField( max_length=4, unique=True )
    name = models.CharField( max_length=64 )
    email = models.EmailField()
    address = models.ForeignKey( 'Address', blank=True )

    def __unicode__( self ):
	return '%s %s' % ( self.number, self.name )   

    def __repr__( self ):
        return '<Center: %s>' % ( self.__unicode__() )


class Address( models.Model ):
    name = models.CharField( max_length=64 )
    contact = models.CharField( max_length=32, blank=True )
    phone = models.CharField( max_length=12 )
    street = models.CharField( max_length=256 )
    street2 = models.CharField( max_length=16, blank=True )
    city = models.CharField( max_length=32 )
    state = models.CharField( max_length=2 )
    zip = models.CharField( max_length=5 )
    
    def __unicode__( self ):
	return self.name 

    def __repr__( self ):
        return '<Address: %s>' % ( self.__unicode__() )


class DeliveryForm( forms.ModelForm ):
    class Meta:
        model = Delivery

